/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or (at your option)
** any later version.

** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.

** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <QDialogButtonBox>

#include "ia_settings_window.h"

IASettingsWindow::IASettingsWindow(QWidget * parent, Qt::WindowFlags flags)
    : QDialog(parent, flags)
{
    mainLayout = new QGridLayout(this);
    serverGroup = new QGroupBox(tr("Server"), this);
    sgPanel = new QGridLayout();
    nameLbl = new QLabel(tr("Server name:"), this);
    nameFld = new QLineEdit(this);
    portLbl = new QLabel(tr("Server port:"), this);
    portFld = new QLineEdit(this);
    statGroup = new QGroupBox(tr("State view"), this);
    statPanel = new QHBoxLayout();
    unitsGroup = new QGroupBox(this);
    unitsButtonGroup = new QButtonGroup(unitsGroup);
    unitsPanel = new QVBoxLayout();

    // Units radiobutton group
    unitsButtonGroup->addButton(floatingBtn = new QRadioButton(tr("Floating")), 0); // 0
    unitsButtonGroup->addButton(bBtn = new QRadioButton(tr("bytes")), 1); // 1
    unitsButtonGroup->addButton(kbBtn = new QRadioButton(tr("Kb")), 2); // 2
    unitsButtonGroup->addButton(mbBtn = new QRadioButton(tr("Mb")), 3); // 3

    floatingBtn->setChecked(true);

    formatGroup = new QGroupBox(this);
    formatButtonGroup = new QButtonGroup(formatGroup);
    formatPanel = new QVBoxLayout();

    // Format radiobutton group
    formatButtonGroup->addButton(upplusdownBtn = new QRadioButton(tr("upload + download"), formatGroup), 0); // 0
    formatButtonGroup->addButton(downBtn = new QRadioButton(tr("download"), formatGroup), 1); // 1
    formatButtonGroup->addButton(updownBtn = new QRadioButton(tr("upload / download"), formatGroup), 2); // 2

    updownBtn->setChecked(true);

    warningGroup = new QGroupBox(tr("Low cash warning"), this);
    warningPanel = new QHBoxLayout();
    flashCB = new QCheckBox(tr("Flash on low cash"), this);
    minCashFld = new QLineEdit(this);
    otherGroup = new QGroupBox(tr("Other settings"), this);
    otherPanel = new QVBoxLayout();
    autoCB = new QCheckBox(tr("Autoconnect"), this);
    savePWCB = new QCheckBox(tr("Save password"), this);
    reconnectCB = new QCheckBox(tr("Reconnect"), this);
    linkHDDCB = new QCheckBox(tr("Link password to HDD"), this);
    minimizeCB = new QCheckBox(tr("Minimize on close"), this);
    hintGroup = new QGroupBox(tr("Hints"), this);
    hintPanel = new QVBoxLayout();
    cashHintCB = new QCheckBox(tr("Cash hint"), this);
    traffHintCB = new QCheckBox(tr("Traffic hint"), this);
    dirLbl = new QLabel(tr("Direction:"), this);
    dirsFld = new QComboBox(this);
    QDialogButtonBox * dialogButtonBox = 
        new QDialogButtonBox(QDialogButtonBox::Ok |
                             QDialogButtonBox::Cancel);

    setModal(true);
    setLayout(mainLayout);

    mainLayout->addWidget(serverGroup, 0, 0);

    serverGroup->setLayout(sgPanel);
    sgPanel->addWidget(nameLbl, 0, 0);
    sgPanel->addWidget(portLbl, 1, 0);
    sgPanel->addWidget(nameFld, 0, 1);
    sgPanel->addWidget(portFld, 1, 1);

    mainLayout->addWidget(statGroup, 1, 0, 2, 1);

    statGroup->setLayout(statPanel);
    statPanel->addWidget(unitsGroup);
    unitsGroup->setLayout(unitsPanel);
    unitsPanel->addWidget(floatingBtn);
    unitsPanel->addWidget(bBtn);
    unitsPanel->addWidget(kbBtn);
    unitsPanel->addWidget(mbBtn);
    unitsPanel->addStretch(1);

    statPanel->addWidget(formatGroup);

    formatGroup->setLayout(formatPanel);
    formatPanel->addWidget(upplusdownBtn);
    formatPanel->addWidget(downBtn);
    formatPanel->addWidget(updownBtn);
    formatPanel->addStretch(1);
    // TODO: waiting realization
    formatGroup->setEnabled(false);

    mainLayout->addWidget(warningGroup, 3, 0);

    warningGroup->setLayout(warningPanel);
    warningPanel->addWidget(flashCB);
    warningPanel->addWidget(minCashFld);
    // TODO: waiting realization
    warningGroup->setEnabled(false);

    mainLayout->addWidget(otherGroup, 0, 1, 2, 1);

    otherGroup->setLayout(otherPanel);
    otherPanel->addWidget(autoCB);
    otherPanel->addWidget(savePWCB);
    otherPanel->addWidget(reconnectCB);
    otherPanel->addWidget(linkHDDCB);
    otherPanel->addWidget(minimizeCB);
    otherPanel->addStretch(1);

    // TODO: waiting realization
    linkHDDCB->setEnabled(false);

    mainLayout->addWidget(hintGroup, 2, 1, 2, 1);

    hintGroup->setLayout(hintPanel);
    hintPanel->addWidget(cashHintCB);
    hintPanel->addWidget(traffHintCB);
    hintPanel->addWidget(dirLbl);
    hintPanel->addWidget(dirsFld);
    hintPanel->addStretch(1);
    // TODO: waiting realization
    hintGroup->setEnabled(false);

    mainLayout->addWidget(dialogButtonBox, 4, 0, 4, 2);

    connect(dialogButtonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(dialogButtonBox, SIGNAL(rejected()), this, SLOT(reject()));
}

void IASettingsWindow::setServerName(const QString & name)
{
    nameFld->setText(name);
}

void IASettingsWindow::setServerPort(int p)
{
    portFld->setText(QString::number(p));
}

void IASettingsWindow::setUnits(int u)
{
    switch (u) {
        case 0:
            floatingBtn->setChecked(true);
            break;
        case 1:
            bBtn->setChecked(true);
            break;
        case 2:
            kbBtn->setChecked(true);
            break;
        case 3:
            mbBtn->setChecked(true);
            break;
    };
}

void IASettingsWindow::setFormat(int f)
{
    switch (f) {
        case 0:
            upplusdownBtn->setChecked(true);
            break;
        case 1:
            downBtn->setChecked(true);
            break;
        case 2:
            updownBtn->setChecked(true);
            break;
    };
}

void IASettingsWindow::setFlashWarning(bool flag)
{
    flashCB->setChecked(flag);
}

void IASettingsWindow::setMinCashWarning(double cash)
{
    minCashFld->setText(QString::number(cash, 'f', 2));
}

void IASettingsWindow::setAutoconnect(bool flag)
{
    autoCB->setChecked(flag);
}

void IASettingsWindow::setSavePassword(bool flag)
{
    savePWCB->setChecked(flag);
}

void IASettingsWindow::setReconnect(bool flag)
{
    reconnectCB->setChecked(flag);
}

void IASettingsWindow::setLinkHDD(bool flag)
{
    linkHDDCB->setChecked(flag);
}

void IASettingsWindow::setMinimize(bool flag)
{
    minimizeCB->setChecked(flag);
}

void IASettingsWindow::setCashHint(bool flag)
{
    cashHintCB->setChecked(flag);
}

void IASettingsWindow::setTraffHint(bool flag)
{
    traffHintCB->setChecked(flag);
}

void IASettingsWindow::setHintDir(int dir)
{
    dirsFld->setCurrentIndex(dir);
}


QString IASettingsWindow::getServerName() const
{
    return nameFld->text();
}

int IASettingsWindow::getServerPort() const
{
    return portFld->text().toInt();
}

int IASettingsWindow::getUnits() const
{
    return unitsButtonGroup->checkedId();
}

int IASettingsWindow::getFormat() const
{
    return formatButtonGroup->checkedId();
}

bool IASettingsWindow::getFlashWarning() const
{
    return flashCB->isChecked();
}

double IASettingsWindow::getMinCashWarning() const
{
    return minCashFld->text().toDouble();
}

bool IASettingsWindow::getAutoconnect() const
{
    return autoCB->isChecked();
}

bool IASettingsWindow::getSavePassword() const
{
    return savePWCB->isChecked();
}

bool IASettingsWindow::getReconnect() const
{
    return reconnectCB->isChecked();
}

bool IASettingsWindow::getLinkHDD() const
{
    return linkHDDCB->isChecked();
}

bool IASettingsWindow::getMinimize() const
{
    return minimizeCB->isChecked();
}

bool IASettingsWindow::getCashHint() const
{
    return cashHintCB->isChecked();
}

bool IASettingsWindow::getTraffHint() const
{
    return traffHintCB->isChecked();
}

int IASettingsWindow::getHintDir() const
{
    return dirsFld->currentIndex();
}

